package com.cema.manage.project.manage.userDailyActivity.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.common.utils.StringUtils;
import com.cema.manage.framework.web.controller.BaseController;
import com.cema.manage.framework.web.domain.Message;
import com.cema.manage.framework.web.page.TableDataInfo;
import com.cema.manage.project.manage.friend.service.IFriendService;
import com.cema.manage.project.manage.userDailyActivity.domain.UserDailyActivity;
import com.cema.manage.project.manage.userDailyActivity.domain.UserDailyActivityVo;
import com.cema.manage.project.manage.userDailyActivity.service.IUserDailyActivityService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 用户日活 信息操作处理
 *
 * @author reasahi
 * @date 2024-08-04
 */
@Controller
@RequestMapping("/manage/userDailyActivity")
public class UserDailyActivityController extends BaseController {
    private String prefix = "manage/userDailyActivity";

    @Autowired
    private IUserDailyActivityService userDailyActivityService;

    @Resource
    private IFriendService iFriendService;

    @GetMapping()
    @RequiresPermissions("manage:userDailyActivity:view")
    public String userDailyActivity() {
        return prefix + "/userDailyActivity";
    }

    @GetMapping("{id}")
    @RequiresPermissions("manage:userDailyActivity:view")
    public String userDailyActivity(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("userId", id);
        return prefix + "/userDailyActivity";
    }

    @GetMapping("/ip")
    @RequiresPermissions("manage:userDailyActivity:view")
    public String userDailyIp(HttpServletRequest request, Model model) {
        model.addAttribute("udaIp", request.getParameter("ip"));
        return prefix + "/userDailyActivity";
    }

    /**
     * 查询用户日活列表
     */
    @RequiresPermissions("manage:userDailyActivity:list")
    @ResponseBody
    @PostMapping("/list")
    public TableDataInfo list(UserDailyActivityVo userDailyActivityVo) {
        EntityWrapper<UserDailyActivity> entityWrapper = new EntityWrapper<>();
        Integer plUserId = getUser().getPlUserId();
        if (ObjectUtil.isNotNull(plUserId)) {
            List<Integer> userIdList = iFriendService.getUserIdList(plUserId, userDailyActivityVo.getUsId());
            if (ObjectUtil.isNull(userIdList)) {
                return getDataTable(new ArrayList<>());
            }
            userDailyActivityVo.setUsId(null);
            entityWrapper.in("us_id", userIdList);
        }
        startPage();
        UserDailyActivity userDailyActivity = BeanUtil.toBean(userDailyActivityVo, UserDailyActivity.class);
        String nickname = userDailyActivityVo.getNickname();
        if (StrUtil.isNotEmpty(nickname)) {
            entityWrapper.like("us_nickname", nickname, SqlLike.RIGHT);
        }
        entityWrapper.setEntity(userDailyActivity);
        Date start = userDailyActivityVo.getStartTime();
        if (ObjectUtil.isNotNull(start)) {
            entityWrapper.andNew();
            entityWrapper.between("create_time", start, userDailyActivityVo.getEndTime());
        }
        String startUdaAllApp = userDailyActivityVo.getStartUdaAllApp();
        String endUdaAllApp = userDailyActivityVo.getEndUdaAllApp();
        if (StrUtil.isNotEmpty(startUdaAllApp) && StrUtil.isNotEmpty(endUdaAllApp)) {
            entityWrapper.between("uda_all_app", startUdaAllApp, Integer.parseInt(endUdaAllApp) + 1);
        }
        if (StrUtil.isNotEmpty(startUdaAllApp)) {
            entityWrapper.ge("uda_all_app", startUdaAllApp);
        }
        if (StrUtil.isNotEmpty(endUdaAllApp)) {
            entityWrapper.le("uda_all_app", endUdaAllApp);
        }
        entityWrapper.orderBy("create_time", false);
        List<UserDailyActivity> list = userDailyActivityService.selectList(entityWrapper);
        return getDataTable(list);
    }

    /**
     * 新增用户日活
     */
    @RequiresPermissions("manage:userDailyActivity:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 修改用户日活
     */
    @RequiresPermissions("manage:userDailyActivity:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model) {
        UserDailyActivity userDailyActivity = userDailyActivityService.selectById(id);
        model.addAttribute("userDailyActivity", userDailyActivity);
        return prefix + "/edit";
    }

    /**
     * 保存用户日活
     */
    @RequiresPermissions("manage:userDailyActivity:save")
    @PostMapping("/save")
    @ResponseBody
    public Message save(UserDailyActivity userDailyActivity) {
        Integer id = userDailyActivity.getId();
        if (StringUtils.isNotNull(id)) {
            if (userDailyActivityService.updateById(userDailyActivity)) {
                return Message.success();
            }
            return Message.error();
        } else {
            if (userDailyActivityService.insert(userDailyActivity)) {
                return Message.success();
            }
            return Message.error();
        }
    }

    /**
     * 删除用户日活
     */
    @RequiresPermissions("manage:userDailyActivity:remove")
    @PostMapping("/remove/{id}")
    @ResponseBody
    public Message remove(@PathVariable("id") Integer id) {
        if (userDailyActivityService.deleteById(id)) {
            return Message.success();
        }
        return Message.error();
    }

    /**
     * 批量删除用户日活
     */
    @RequiresPermissions("manage:userDailyActivity:batchRemove")
    @PostMapping("/batchRemove")
    @ResponseBody
    public Message remove(@RequestParam("ids[]") Integer[] ids) {
        List list = new ArrayList();
        for (Integer n : ids) {
            list.add(n);
        }
        if (userDailyActivityService.deleteBatchIds(list)) {
            return Message.success();
        }
        return Message.error();
    }

    @GetMapping("/my")
    @RequiresPermissions("manage:userDailyActivity:view")
    public String userDailyActivityMy() {
        return prefix + "/userDailyActivityMy";
    }

    @GetMapping("/my/{id}")
    @RequiresPermissions("manage:userDailyActivity:view")
    public String userDailyActivityMyId(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("usId", id);
        return prefix + "/userDailyActivityMy";
    }

    @GetMapping("/id/{id}")
    public String action(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("id", id);
        return prefix + "/userDailyActivityAll";
    }

    /**
     * 查询用户日活列表
     */
    @ResponseBody
    @PostMapping("/list_all")
    public TableDataInfo listAll(UserDailyActivityVo userDailyActivityVo) {
        UserDailyActivity userDailyActivity = userDailyActivityService.selectById(userDailyActivityVo.getId());
        String udaAllAppDetail = userDailyActivity.getUdaAllAppDetail();
        List<UserDailyActivity> userDailyActivityList = JSONUtil.toList(udaAllAppDetail, UserDailyActivity.class);
        return getDataTable(userDailyActivityList);
    }

}
